<?php $title = "管理员"; ?>
<!DOCTYPE html>
<html>

<head>
    <?php include('css.php'); ?>
</head>

<body>
    <?php include('nav.php'); ?>
    <div class="main-content">
        <div class="container-fluid">
            <div class="row mb-4">
                <div class="col">
                    <h1 class="page-title"><?php echo $title; ?></h1>
                    <div class="mt-3">
                        <a href="admin.php?menu=add_admin" class="btn btn-primary">添加管理员</a>
                        <a href="admin.php?menu=list_admin" class="btn btn-primary">列出管理员</a>
                        <a href="admin.php?menu=admin_log" class="btn btn-primary">操作日志</a>
                    </div>
                    <div class="mt-3 mb-3">
                        <?php
                        $uid = $_SESSION['id'];
                        if (isset($_GET['menu'])) {
                            $menu = $_GET['menu'];
                            switch ($menu) {
                                case 'add_admin':
                        ?>
                                    <form method="POST" action="toset.php?action=user">
                                        <div class="mb-3">
                                            <label class="form-label" for="username">管理用户名</label>
                                            <input class="form-control form-control-lg" name="username" id="username">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label" for="password">管理密码</label>
                                            <input class="form-control form-control-lg" name="password" id="password">
                                        </div>
                                        <div class="mb-3">
                                            <input type="submit" class="btn btn-primary" value="添加管理员">
                                        </div>
                                    </form>
                                <?php
                                    break;


                                case 'admin_log':
                                    $admin_log = "select * from admin_log order by time desc";
                                    $stmt = $link->prepare($admin_log);
                                    $stmt->execute();
                                    $result = $stmt->get_result();
                                ?>
                                    <style>
                                        .log-row:hover {
                                            background-color: #f8f9fa;
                                            transition: background-color 0.2s;
                                        }

                                        .ip-badge {
                                            font-family: monospace;
                                            background-color: #f1f1f1;
                                            padding: 3px 6px;
                                            border-radius: 4px;
                                        }

                                        .time-col {
                                            white-space: nowrap;
                                        }
                                    </style>
                                    <?php
                                    $admin_log = "select * from admin_log order by time desc";
                                    $stmt = $link->prepare($admin_log);
                                    $stmt->execute();
                                    $result = $stmt->get_result();
                                    ?>

                                    <div class="container-fluid py-4">
                                        <div class="d-flex justify-content-between align-items-center mb-4">
                                            <div class="d-flex">
                                                <input type="text" class="form-control me-2" placeholder="搜索操作内容..." id="searchInput">
                                                <button class="btn btn-outline-secondary" type="button">
                                                    筛选
                                                </button>
                                            </div>
                                        </div>

                                        <div class="table-responsive">
                                            <table class="table table-hover align-middle">
                                                <thead class="table-light">
                                                    <tr>
                                                        <th width="80px">ID</th>
                                                        <th width="180px">IP地址</th>
                                                        <th>操作内容</th>
                                                        <th class="time-col">时间</th>
                                                    </tr>
                                                </thead>
                                                <tbody>
                                                    <?php while ($log_row = $result->fetch_assoc()) { ?>
                                                        <tr class="log-row">
                                                            <td class="fw-bold">#<?php echo $log_row['id']; ?></td>
                                                            <td><span class="ip-badge"><?php echo $log_row['ip']; ?></span></td>
                                                            <td><?php echo htmlspecialchars($log_row['text']); ?></td>
                                                            <td class="time-col"><i class="far fa-clock me-1"></i><?php echo date("Y-m-d H:i:s", $log_row['time']) ?></td>
                                                        </tr>
                                                    <?php } ?>
                                                </tbody>
                                            </table>
                                        </div>

                                    </div>

                                    <script>
                                        document.getElementById('searchInput').addEventListener('input', function(e) {
                                            const searchTerm = e.target.value.toLowerCase();
                                            document.querySelectorAll('tbody tr').forEach(row => {
                                                const text = row.textContent.toLowerCase();
                                                row.style.display = text.includes(searchTerm) ? '' : 'none';
                                            });
                                        });
                                    </script>
                                <?php
                                    break;

                                case 'list_admin':
                                    $list = "select * from admin_user";
                                    $stmt = $link->prepare($list);
                                    $stmt->execute();
                                    $result = $stmt->get_result();
                                ?>
                                    <div class="mt-3 mb-3">
                                        <div class="row align-items-center mt-3 mb-3">
                                            <div class="col">
                                                <h5>ID</h5>
                                            </div>
                                            <div class="col">
                                                <h5>管理员账号</h5>
                                            </div>
                                            <div class="col">
                                                <h5>管理员密码</h5>
                                            </div>
                                            <div class="col">
                                                <h5>管理员操作</h5>
                                            </div>
                                        </div>
                                        <?php while ($list_row = $result->fetch_assoc()) { ?>
                                            <div class="row align-items-center mt-3 mb-3">
                                                <div class="col"><?php echo $list_row['id']; ?></div>
                                                <div class="col"><?php echo $list_row['username']; ?></div>
                                                <div class="col"><?php echo $list_row['password']; ?></div>
                                                <div class="col">
                                                    <a href="JavaScript:;" onclick="if(confirm('包纸提示：您确定要删除 {<?php echo $list_row['name']; ?>} 这个管理员吗？')){ actionControl('deladmin', '<?php echo $list_row['id']; ?>'); }" class="btn btn-danger">删除</a>
                                                </div>
                                            </div>
                                        <?php } ?>
                                    </div>
                                    <script type="text/javascript">
                                        function actionControl(actionType, adminid) {
                                            var xhr = new XMLHttpRequest();
                                            var requestUrl = 'toset.php?action=' + encodeURIComponent(actionType) + '&adminid=' + encodeURIComponent(adminid);
                                            xhr.open('GET', requestUrl, true);
                                            xhr.onreadystatechange = function() {
                                                if (xhr.readyState === 4) {
                                                    if (xhr.status === 200) {
                                                        location.reload();
                                                    } else {
                                                        alert('出错！状态码：' + xhr.status);
                                                    }
                                                }
                                            };
                                            xhr.send();
                                        }
                                    </script>

                        <?php break;
                            }
                        } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include('bottom.php'); ?>
</body>

</html>